package org.unt.bean;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Bean {

	/**
	 * toString() method implemented using reflection. Format > property1 : value1, property2 : value2, ...
	 */
	public String toString() {

		StringBuffer buf = new StringBuffer();

		Method[] methods = this.getClass().getMethods();

		for (int i = 0; i < methods.length; i++) {

			if (methods[i].getName().startsWith("get") && !methods[i].getName().equals("getClass")) {

				Object obj = null;

				try {

					obj = methods[i].invoke(this, (Object[]) null);

				} catch (IllegalArgumentException e) {
					return e.getMessage();
				} catch (IllegalAccessException e) {
					return e.getMessage();
				} catch (InvocationTargetException e) {
					return e.getMessage();
				}

				buf.append(methods[i].getName().substring(3, 4).toLowerCase() + methods[i].getName().substring(4) + " : ");

				if (obj != null) {
					buf.append(obj + ", ");
				} else {
					buf.append("[null], ");
				}
			}
		}

		return buf.toString().substring(0, buf.toString().length() - 2);
	}
}
